package com.minhua.jijiehao2.base.adapter.abslistview;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import com.minhua.jijiehao2.base.adapter.ViewHolder;

import java.util.List;

/**
 * @author moo
 * @date 2017/11/27.
 * @describe ExpandableListView adapter
 */

public abstract class BaseExpCommonAdapter<C, G extends BaseExpCommonAdapter.BaseExpBean<C>> extends BaseExpandableListAdapter {
    private Context context;
    private List<G> data;
    private int groupLayoutId;
    private int childLayoutId;

    public BaseExpCommonAdapter(Context context, int groupLayoutId, int childLayoutId, List<G> data) {
        this.context = context;
        this.data = data;
        this.groupLayoutId = groupLayoutId;
        this.childLayoutId = childLayoutId;
    }

    @Override
    public int getGroupCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data == null ? 0 :
                (data.get(groupPosition) == null ? 0 :
                        data.get(groupPosition).getList() == null ? 0 :
                                data.get(groupPosition).getList().size());
    }

    @Override
    public G getGroup(int groupPosition) {
        return data == null ? null : data.get(groupPosition);
    }

    @Override
    public C getChild(int groupPosition, int childPosition) {
        return data == null ? null :
                (data.get(groupPosition) == null ? null :
                        (data.get(groupPosition).getList() == null ? null :
                                data.get(groupPosition).getList().get(childPosition)));
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder groupViewHolder = ViewHolder.get(context, convertView, parent, groupLayoutId, groupPosition);
        convertGroup(groupViewHolder, getGroup(groupPosition));
        return groupViewHolder.getConvertView();
    }


    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ViewHolder childViewHolder = ViewHolder.get(context, convertView, parent, childLayoutId, childPosition);
        convertChild(childViewHolder, getChild(groupPosition, childPosition));
        return childViewHolder.getConvertView();
    }

    /**
     * 绑定group
     */
    protected abstract void convertGroup(ViewHolder groupViewHolder, G group);

    /**
     * 绑定child
     */
    protected abstract void convertChild(ViewHolder childViewHolder, C child);

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    public abstract static class BaseExpBean<T> {
        /**
         * 获取Parent下的Child列表
         */
        public abstract List<T> getList();
    }
}
